package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.util.List;

import com.ruoyi.system.domain.SysRural;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysIll;
import com.ruoyi.system.service.ISysIllService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 慢病分类管理Controller
 *
 * @author moweiyu
 * @date 2021-04-30
 */
@Controller
@RequestMapping("/system/ill")
public class SysIllController extends BaseController
{
    private String prefix = "system/ill";

    @Autowired
    private ISysIllService sysIllService;

    @RequiresPermissions("system:ill:view")
    @GetMapping()
    public String ill()
    {
        return prefix + "/ill";
    }

    /**
     * 查询慢病分类管理列表
     */
    @RequiresPermissions("system:ill:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysIll sysIll)
    {
        startPage();
        List<SysIll> list = sysIllService.selectSysIllList(sysIll);
        return getDataTable(list);
    }

    /**
     * 通过 ruralName 查询行政区域 Ajax （新增页面）
     */
    @RequiresPermissions("system:rural:list")
    @PostMapping("/listAjax")
    @ResponseBody
    public void listAjax(HttpServletResponse response, SysIll sysIll) throws IOException {

        List<SysIll> list = sysIllService.selectSysIllList(sysIll);

        String data = null;
        if (list.size() != 0) {
            data = "{\"res\":\"fail\"}";
        } else {
            data = "{\"res\":\"success\"}";
        }

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().write(data);
    }

    /**
     * 导出慢病分类管理列表
     */
    @RequiresPermissions("system:ill:export")
    @Log(title = "慢病分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysIll sysIll)
    {
        List<SysIll> list = sysIllService.selectSysIllList(sysIll);
        ExcelUtil<SysIll> util = new ExcelUtil<SysIll>(SysIll.class);
        return util.exportExcel(list, "慢病分类管理数据");
    }

    /**
     * 新增慢病分类管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存慢病分类管理
     */
    @RequiresPermissions("system:ill:add")
    @Log(title = "慢病分类管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysIll sysIll)
    {
        return toAjax(sysIllService.insertSysIll(sysIll));
    }

    /**
     * 修改慢病分类管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysIll sysIll = sysIllService.selectSysIllById(id);
        mmap.put("sysIll", sysIll);
        return prefix + "/edit";
    }

    /**
     * 修改保存慢病分类管理
     */
    @RequiresPermissions("system:ill:edit")
    @Log(title = "慢病分类管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysIll sysIll)
    {
        return toAjax(sysIllService.updateSysIll(sysIll));
    }

    /**
     * 删除慢病分类管理
     */
    @RequiresPermissions("system:ill:remove")
    @Log(title = "慢病分类管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysIllService.deleteSysIllByIds(ids));
    }
}
